package com.xjscrm.console.chat.core.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjscrm.common.entity.WwGroup;
import com.xjscrm.common.entity.WwGroupMember;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RPullGroupMemberMsg;
import com.xjscrm.console.chat.frame.send.SPullGroupMemberResultMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.group.WwGroupService;
import com.xjscrm.console.vo.chat.ChatGroupMemberVO;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/25 15:19
 */
@Component
@Slf4j
public class ChatPullGroupMemberMsgHandler extends ChatBaseMsgHandler<RPullGroupMemberMsg> {

    @Autowired
    private WwGroupService groupService;
    @Autowired
    ChatTalkSessionService chatTalkSessionService;

    @Override
    public Class<RPullGroupMemberMsg> msgClass() {
        return RPullGroupMemberMsg.class;
    }

    @Override
    public SResultMsg doHandle(RPullGroupMemberMsg content, ChannelHandlerContext ctx) {

        SPullGroupMemberResultMsg pullGroupMemberResultMsg = new SPullGroupMemberResultMsg();
        BeanUtils.copyProperties(content, pullGroupMemberResultMsg);

        Long talkSessionId = content.getTalkSessionId();
        ChatTalkSession byId = chatTalkSessionService.getById(content.getMerId(), talkSessionId);
        if (Objects.isNull(byId)) {
            throw new BusinessException("获取群资料失败: 会话不存在");
        }

        WwGroup group = groupService.getByGroupId(content.getMerId(), content.getDeviceUniqueId(), byId.getConversationId());
        if (Objects.isNull(group)) {
            throw new BusinessException("群聊信息丢失, 请稍后重试!");
        }
        String ownerWxId = group.getCreateWxid();
        List<String> adminWxIds = null;
        boolean hasAdmin = false;
        if (StringUtils.isNotBlank(group.getAdminWxIds())) {
            adminWxIds = JSON.parseArray(group.getAdminWxIds(), String.class);
            hasAdmin = true;
        }
        //
        Page<WwGroupMember> memberList = groupService.listMembersFromChat(content);

        List<ChatGroupMemberVO> resultRecord = new ArrayList<>();
        for (WwGroupMember record : memberList.getRecords()) {

            ChatGroupMemberVO chatGroupMemberVO = new ChatGroupMemberVO();
            chatGroupMemberVO.setWxid(record.getWxid());
            chatGroupMemberVO.setNick(record.getNick());
            chatGroupMemberVO.setHeadImgUrl(record.getHimg());
            chatGroupMemberVO.setOwner(StringUtils.equals(record.getWxid(), ownerWxId) ? 1 : 0);
            if (hasAdmin) {
                chatGroupMemberVO.setAdmin(adminWxIds.contains(record.getWxid()) ? 1 : 0);
            }
            resultRecord.add(chatGroupMemberVO);
        }

        Page<ChatGroupMemberVO> result = new Page<ChatGroupMemberVO>(memberList.getCurrent(), memberList.getSize(), memberList.getTotal()).setRecords(resultRecord);
        pullGroupMemberResultMsg.setMemberPage(result);
        return pullGroupMemberResultMsg;
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_pull_group_conversation_members;
    }
}
